package com.ruoyi.course.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.vo.ExamInvoiceVo;
import com.ruoyi.course.service.IExamInvoiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 发票申请Controller
 *
 * @author Tellsea
 * @date 2025-10-14
 */
@Api(tags = "发票申请Controller")
@RestController
@RequestMapping("/course/invoice")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ExamInvoiceController extends BaseController {

    private final IExamInvoiceService examInvoiceService;

    @ApiOperation("查询发票申请列表")
    @PreAuthorize("@ss.hasPermi('course:invoice:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamInvoiceVo> list(ExamInvoiceVo entity) {
        return examInvoiceService.queryList(entity);
    }

    @ApiOperation("查询发票申请所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ExamInvoiceVo entity) {
        return AjaxResult.success("查询成功", examInvoiceService.queryAll(entity));
    }

    @ApiOperation("查询我的发票申请记录列表")
    @GetMapping("/myList")
    public AjaxResult myList() {
        Long userId = SecurityUtils.getUserId();
        List<ExamInvoiceVo> list = examInvoiceService.queryMyInvoiceList(userId);
        return AjaxResult.success("查询成功", list);
    }

    @ApiOperation("分页查询我的开票记录")
    @GetMapping("/myPage")
    public TableDataInfo<ExamInvoiceVo> myPage() {
        Long userId = SecurityUtils.getUserId();
        return examInvoiceService.queryMyInvoicePage(userId);
    }

    @ApiOperation("导出发票申请列表")
    @PreAuthorize("@ss.hasPermi('course:invoice:export')")
    @Log(title = "发票申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamInvoiceVo entity) {
        List<ExamInvoiceVo> list = examInvoiceService.queryAll(entity);
        ExcelUtil<ExamInvoiceVo> util = new ExcelUtil<>(ExamInvoiceVo.class);
        util.exportExcel(response, list, "发票申请数据");
    }

    @ApiOperation("获取发票申请详细信息")
    @PreAuthorize("@ss.hasPermi('course:invoice:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", examInvoiceService.queryById(id));
    }

    @ApiOperation("新增发票申请")
    @PreAuthorize("@ss.hasPermi('course:invoice:add')")
    @Log(title = "发票申请", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ExamInvoiceVo entity) {
        // 检查是否已存在相同身份证和班次的申请
        if (examInvoiceService.existsByIdCardAndClassId(getUserId(), entity.getClassId())) {
            return AjaxResult.error("该身份证号在此班次已存在发票申请，无法重复申请");
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        entity.setUserId(user.getUserId());
        entity.setApplicantName(user.getNickName());
        entity.setApplicantPhone(user.getPhonenumber());
        entity.setApplicantIdCard(user.getIdCardNumber());
        return toAjax(examInvoiceService.save(entity));
    }

    @ApiOperation("修改发票申请")
    @PreAuthorize("@ss.hasPermi('course:invoice:edit')")
    @Log(title = "发票申请", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ExamInvoiceVo entity) {
        return toAjax(examInvoiceService.updateById(entity));
    }

    @ApiOperation("删除发票申请")
    @PreAuthorize("@ss.hasPermi('course:invoice:remove')")
    @Log(title = "发票申请", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(examInvoiceService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
